package book.manage.sql;

import book.manage.entity.Book;
import book.manage.mapper.BookMapper;
import com.sun.xml.internal.ws.model.ReflectAnnotationReader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.util.function.Consumer;

/**
 * @author 年年
 * 我是一个工具类
 */
public class SqlUtil {

    private static  SqlSessionFactory factory;
    static {
        try{
            factory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     *
     * 这边简化一下，获取SqlSession我们自己做了,BookMapper也自己做，然后他直接可以用BookMapper去做事情就行
     *
     */
    public  static void doSqlwork(Consumer<BookMapper> consumer){
        try(SqlSession sqlSession=factory.openSession(true)){
            BookMapper bookMapper=sqlSession.getMapper(BookMapper.class);
            consumer.accept(bookMapper);//这里直接传个函数式就可以了
        }
    }
}
